package com.awardsystem.Controller;

import com.alibaba.fastjson.JSONObject;
import com.awardsystem.Service.UserService;
import com.awardsystem.entity.*;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;

/*
* 这个类主要用来对用户进行管理
* */
@RestController
@CrossOrigin
public class UserController {
    @Autowired
    UserService userService;
    //public ConfigurableApplicationContext run;
    @Value("${admin.username}")
    String adminUsername;
    @Value("${admin.password}")
    String adminPassword;
    /**
     * 登录功能：账号密码 admin
     * @param params
     * @return
     */
    @PostMapping("/login")
    public JSONObject login(@RequestBody JSONObject params){
        JSONObject ret = new JSONObject();
        String username = params.getString("username");
        String password = params.getString("password");
        if (!adminUsername.equals(username)){
            ret.put("success", false);
            ret.put("exc", "用户名不存在");
        }
        if (!adminPassword.equals(password)){
            ret.put("success", false);
            ret.put("exc", "用户密码错误");
        }
        if (adminUsername.equals(username) && adminPassword.equals(password)){
            ret.put("success", true);
            ret.put("exc", "用户登录成功");
        }
        return ret;
    }

    //增加一个用户，如果用户学号已存在，则认为重复。
    @PostMapping("/addUser")
    public JSONObject addUser(@RequestBody JSONObject params){
        JSONObject ret=new JSONObject();
        String number=params.getString("number");
        String personName = params.getString("personName");
        String profile = params.getString("profile");
        if(userService.findUserByNumber(number)!=null) {
            ret.put("success", false);
            ret.put("exc", "禁止重复添加");
            return ret;
        }
        User user=new User();
        user.setNumber(number);
        user.setPersonName(personName);
        user.setProfile(profile);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(user.getCreateTime());
        user.setGraduated(false);
        user.setGraduatedRar("http://8.140.124.245:10000/resources/");
        userService.addUser(user);
        ret.put("success",true);
        ret.put("exc","success");
        return ret;
    }
    @PostMapping("/getAllUsers")
    public JSONObject GetAllUsers(){
        JSONObject ret=new JSONObject();
        List<User> userList = userService.getAllUsers();

        ret.put("success",true);
        ret.put("exc","success");
        ret.put("list",userList);
        return ret;
    }

    @PostMapping("/SearchByUserName")
    public JSONObject SearchByUserName(@RequestBody JSONObject params){
        String name = params.getString("name");
        JSONObject ret = new JSONObject();
        List<User> userList = userService.getUsersByName(name);

        ret.put("success",true);
        ret.put("exc","success");
        ret.put("list",userList);
        return ret;
    }

    @PostMapping("/DelUserById")
    public JSONObject DelUserById(@RequestBody JSONObject params){
        Integer id = params.getInteger("id");
        JSONObject ret=new JSONObject();
        User user = userService.findUserById(id);
        if(user == null) {
            ret.put("success", false);
            ret.put("exc", "用户不存在");
            return ret;
        }
        userService.deleteUser(user);
        ret.put("success", true);
        ret.put("exc", "success");
        return ret;
    }

    @PostMapping("/UpdateUser")
    public JSONObject UpdateUser(@RequestBody JSONObject params){
        JSONObject ret=new JSONObject();
        Integer id = params.getInteger("id");
        String number=params.getString("number");
        String personName = params.getString("personName");
        String profile = params.getString("profile");
        Boolean graduated=params.getBoolean("graduated");
        String url=params.getString("graduatedRar");
        User user = userService.findUserById(id);
        if (user == null){
            ret.put("success", false);
            ret.put("exc", "用户不存在");
            return ret;
        }
        user.setNumber(number);
        user.setProfile(profile);
        user.setPersonName(personName);
        user.setUpdateTime(LocalDateTime.now());
        user.setGraduated(graduated);
        if(!user.isGraduated()&&url!=null){
            ret.put("success",false);
            ret.put("exc","未毕业，不允许上传材料");
            return ret;
        }
        user.setGraduatedRar("http://8.140.124.245:10000/resources/"+url);
        userService.addUser(user);
        ret.put("success",true);
        ret.put("exc","success");
        return ret;
    }





}
